import {http} from '../utils/http'
import type {Equipment } from '../types/equipment'
//查询检测列表
export const getTaskList=()=>{
  return http<Equipment[]>({
    method: 'GET',
    url: `/eduservice/businessmanEquipment/getTaskList`,
  })
}
//修改预约时间(新增检测报告表)
export const AddDetectionReport=(form:any)=>{
  return http({
    method: 'POST',
    url: `/eduservice/detectionReport/AddDetectionReport`,
    data: form,
  })
}

/**
 * 上传头像到 OSS
 * @param {string} filePath - 文件临时路径，如 tempFilePath
 * @returns {Promise<any>}
 */
export const uploadAvatarToOss = (filePath: string) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: '/serviceoss/fileoss/uploadAvatarToOss', // 确保域名合法或已关闭校验
      filePath: filePath,
      name: 'file', // 后端接收字段名
      header: {
        'token': uni.getStorageSync('token'), // 可选：携带登录凭证
        'Content-Type': 'multipart/form-data'
      },
      success: (res) => {
        let data
        try {
          data = JSON.parse(res.data) // 成功后解析响应
        } catch (e) {
          reject(new Error('服务器返回格式错误'))
          return
        }
        if (data.code === 20000) {
          resolve(data)
        } else {
          reject(new Error(data.message || '上传失败'))
        }
      },
      fail: (err) => {
        reject(new Error('网络错误或上传失败'))
      }
    })
  })
}

/**
 * 上传视频到阿里云点播
 * @param {string} filePath - 视频临时路径
 * @returns {Promise<any>}
 */
export const uploadAlyiVideo = (filePath: string) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: '/servicevod/vod/uploadAlyiVideo2',
      filePath: filePath,
      name: 'file',
      header: {
        'token': uni.getStorageSync('token'),
        'Content-Type': 'multipart/form-data'
      },
      success: (res) => {
        let data
        try {
          data = JSON.parse(res.data)
        } catch (e) {
          reject(new Error('服务器返回格式错误'))
          return
        }
        if (data.code === 20000) {
          resolve(data)
        } else {
          reject(new Error(data.message || '视频上传失败'))
        }
      },
      fail: (err) => {
        reject(new Error('视频上传失败，请检查网络'))
      }
    })
  })
}
//审核上传(生成排序为0的检测报告图片)
export const addDetectionReportImage=(data:any)=>{
  return http({
    method: 'POST',
    url: `/eduservice/detectionReportImage/addDetectionReportImage`,
    data
  })
}
//检测报告图片展示
export const getDetectionReportImage=(reportId:any)=>{
  return http({
    method: 'GET',
    url: `/eduservice/detectionReportImage/getDetectionReportImage/${reportId}`,
  })
}
